/*
 * Copyright (C) 2007-2008 SlideMe client
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.android.manager;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import org.slideme.android.manager.discovery.ContentBundles;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Activity that displays detailed information about a content bundle.
 */
public class ApplicationDisplayActivity extends Activity {

    protected static ContentBundles CONTENT_BUNDLES;

    protected static int CONTENT_ID;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.application_display_layout);

        SamApplication samApplication = (SamApplication) getApplication();
        if (samApplication == null) {
            Log.e("ApplicationDisplayActivity.onCreate", "Sam Application not initialized");
            return;
        }

        ApplicationManagerContext ctx = samApplication.getApplicationManagerContext();
        final Cursor c = ctx.getContentBundleRepository().fetchCursorById(CONTENT_ID);
        c.first();
        setTextOnView(c, "displayName", R.id.application_display_displayName);
        setTextOnView(c, "vendorName", R.id.application_display_vendor_name);
        setTextOnView(c, "longDescription", R.id.application_display_longDescription);
        setTextOnView(c, "creationDate", R.id.application_display_date);
        setTextOnView(c, "category", R.id.application_display_category);

        ImageView iconImageView = (ImageView) findViewById(R.id.application_display_image);
        String fileUri = "/data/data/org.slideme.android.manager/files/"
                + c.getString(c.getColumnIndex("iconFileUri"));
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(fileUri.toString()), 8192);
        } catch (FileNotFoundException e) {
            Log.i("failed to read file", fileUri.toString(), e);
        }
        Bitmap bm = BitmapFactory.decodeStream(bis);
        try {
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        iconImageView.setImageBitmap(bm);

        ImageButton returnButton = (ImageButton) findViewById(R.id.application_display_return);
        returnButton.setOnClickListener(new ImageButton.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(ApplicationDisplayActivity.this, TopApplicationsActivity.class);
                startActivity(intent);
            }
        }
        );

        ImageButton imageButton = (ImageButton) findViewById(R.id.application_display_terms);
        imageButton.setOnClickListener(new ImageButton.OnClickListener() {
            public void onClick(View view) {
                TermsAndConditionsActivity.TERMS = c.getString(c.getColumnIndex("termsAndConditions"));
                TermsAndConditionsActivity.CONTENT_URI = c.getString(c.getColumnIndex("contentURI"));
                TermsAndConditionsActivity.BUNDLE_ID = c.getString(c.getColumnIndex("bundleId"));
                Intent intent = new Intent();
                intent.setClass(ApplicationDisplayActivity.this, TermsAndConditionsActivity.class);
                startSubActivity(intent, 1);
            }
        }
        );

        ImageButton myAppsView = (ImageButton) this.findViewById(R.id.application_display_layout_myapps_button);
        ImageButton settingsView = (ImageButton) this.findViewById(R.id.application_display_layout_settings_button);
        ImageButton applicationsView = (ImageButton) this.findViewById(R.id.application_display_layout_applications_button);
        ViewContext.setMenuViewListeners(myAppsView, settingsView, applicationsView, this);
        //     iconImageView.setImageURI(Uri.fromFile(new File("/data/data/org.slideme.android.manager/files/"
        //             + c.getString(c.getColumnIndex("iconFileUri")))));

    }

    private void setTextOnView(Cursor c, String columnName, int resourceId) {
        TextView v = (TextView) this.findViewById(resourceId);
        v.setText(c, c.getColumnIndex(columnName));
    }
}
